#include <iostream>

class AbstractPlayer;
class Mediator // 中介者抽象类
{
public:
    virtual void SendMsg(const std::string &msg, AbstractPlayer *p) = 0;
};

class AbstractPlayer // 抽象用户
{
protected:
    Mediator *m;
    std::string name;

public:
    AbstractPlayer(const std::string &name, Mediator *m) : m(m) {}
    virtual void SendMsg(const std::string &msg) = 0;
    virtual void GetdMsg(const std::string &msg) = 0;
};

class SellPerson : public AbstractPlayer // 卖房子的人
{
public:
    using AbstractPlayer::AbstractPlayer;
    void SendMsg(const std::string &msg)
    {
        m->SendMsg(msg, this); // 当前用户给中介者发消息
    }

    void GetdMsg(const std::string &msg)
    {
        std::cout << name << "收到消息" << msg << std::endl;
    }
};

class BuyPerson : public AbstractPlayer // 买房子的人
{
public:
    using AbstractPlayer::AbstractPlayer;
    void SendMsg(const std::string &msg)
    {
        m->SendMsg(msg, this); // 当前用户给中介者发消息
    }

    void GetdMsg(const std::string &msg)
    {
        std::cout << name << "收到消息" << msg << std::endl;
    }
};

class ConcreMediator : public Mediator
{
private:
    AbstractPlayer *p1;
    AbstractPlayer *p2;

public:
    void SetPlayer(AbstractPlayer *p1, AbstractPlayer *p2)
    {
        this->p1 = p1;
        this->p2 = p2;
    }

    void SendMsg(const std::string &msg, AbstractPlayer *p)
    {
        if (p == p1)
        {
            p2->GetdMsg(msg);
        }
        else
        {
            p1->GetdMsg(msg);
        }
    }
};

int main()
{
    ConcreMediator *m = new ConcreMediator;
    AbstractPlayer *p1 = new SellPerson("卖房子1", m);
    AbstractPlayer *p2 = new SellPerson("卖房子2", m);

    AbstractPlayer *p3 = new BuyPerson("买房子1", m);
    AbstractPlayer *p4 = new BuyPerson("买房子2", m);

    m->SetPlayer(p1, p4);
    p1->SendMsg("你好,房子卖200万"); // 将本需要通信的建立联系的两个类进行解耦

    return 0;
}